/**
 * 更新缺陷状态
 *
 * Created by yangyunze on 2021/04/06.
 */
const chai = require('chai');
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const defects = require('./../../../../phoenix/models/defects');
const uri = require('./../uri');
const benchmark = require('./../../../benchmark/static-analyze/gjb.json');

module.exports = describe('/PUT /project-versions/:projectVersionId/defects/update.json', function () {
  const administrator = new Administrator();
  const defectStatus = defects.defectStatus.edited;
  let project = {};
  let projectVersion = {};
  let newUser;

  function url(projectVersionId) {
    return `/project-versions/${projectVersionId}/defects/update.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUser = tester;
        return newUser.createRandomProject('c++');
      })
      .then((res) => {
        project = res;
        return project.createStaticAnalyzeVersion(
          benchmark.verified_rules,
          benchmark.files,
          newUser
        );
      })
      .then((res) => {
        projectVersion = res;
        return projectVersion.rootDirectory.staticAnalyze();
      })
      .then(() => projectVersion.project.creator.waiting4TasksOfVersionFinished(
        projectVersion.versionId
      ))
      .then(done)
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers([newUser.userId])
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'put');
  common.should.be.tester(uri, url, 'put');

  // 更新缺陷状态
  it('it should update defect status successfully', function (done) {
    new Promise((resolve, reject) => {
      chai.request(uri)
        .get(`/project-versions/${projectVersion.versionId}/defects.json`)
        .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
        .end((err, res) => {
          if (err) {
            reject();
          }
          resolve(res.body.defectsList[0].defectId.toString());
        });
    })
      .then((defectId) => new Promise((resolve, reject) => {
        chai.request(uri)
          .put(url(projectVersion.versionId))
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .send({ status: defectStatus, defectIds: [defectId] })
          .end((err, res) => {
            if (err) {
              reject();
            }
            res.should.have.status(204);
            resolve(defectId);
          });
      }))
      .then((defectId) => {
        chai.request(uri)
          .get(`/project-versions/${projectVersion.versionId}/defects.json`)
          .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
          .end((err, res) => {
            res.should.have.status(200);
            res.body.defectsList.forEach((defect) => {
              if (defect.defectId.toString() !== defectId) {
                return;
              }
              defect.status.should.equal(defectStatus);
            });
            done();
          });
      })
      .catch(done);
  });
});
